package com.eos.common.tool;

import java.util.List;

/**
 * 分页公共类;<br />
 * 对内存中的集合(List)进行分页.
 * @author qiushui
 *
 */
public class PageModelUtil {
    private int pageNum = 1; // 当前页
    public int totalPages = 0; // 总页数
    private int pageRecorders;// 每页显示多少条数据
    private int totalRows = 0; // 总数据数
    private int pageStartRow = 0;// 每页的起始数
    private int pageEndRow = 0; // 每页显示数据的终止数
    private boolean hasNextPage = false; // 是否有下一页
    private boolean hasPreviousPage = false; // 是否有前一页
    private List<?> list;
    
    public PageModelUtil(List<?> list, int pageRecorders) {
        init(list, pageRecorders);// 通过对象集，记录总数划分
    }
   
    /** 
     * 初始化list，并告之该list每页的记录数
     * @param list
     * @param pageRecorders
     */
    public void init(List<?> list, int pageRecorders) {
        this.pageRecorders = pageRecorders;
        this.list = list;
        totalRows = null == list ? 0 : list.size();
        hasPreviousPage = false;
        if ((totalRows % pageRecorders) == 0) {
            totalPages = totalRows / pageRecorders;
        } else {
            totalPages = totalRows / pageRecorders + 1;
        }
        if (pageNum >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        if (totalRows < pageRecorders) {
            this.pageStartRow = 0;
            this.pageEndRow = totalRows;
        } else {
            this.pageStartRow = 0;
            this.pageEndRow = pageRecorders;
        }
    }
    
    /**
     * 获取下一页的数据
     * @return
     */
    public List<?> getNextPage() {
        pageNum = pageNum + 1;
        disposePage();
        return getCurrentPage(pageNum);
    }

    /** 
     * 处理分页
     */
    private void disposePage() {
        if (pageNum == 0) {
            pageNum = 1;
        }
        if ((pageNum - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (pageNum >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }

    /**
     * 获取前一页的内容
     * @return
     */
    public List<?> getPreviousPage() {
    	pageNum = pageNum - 1;
        if ((pageNum - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }
        if (pageNum >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
        return getCurrentPage(pageNum);
    }

    /**
     * 获取第几页的内容
     *
     * @param page 页码
     * @return
     */
    public List<?> getCurrentPage(int page) {
        if (page == 0)
            this.setPage(1);
        else
            this.setPage(page);
        this.disposePage();
        if (pageNum * pageRecorders < totalRows) {// 判断是否为最后一页
            pageEndRow = pageNum * pageRecorders;
            pageStartRow = pageEndRow - pageRecorders;
        } else {
            pageEndRow = totalRows;
            pageStartRow = pageRecorders * (totalPages - 1);
        }
        List<?> resultList = null;
        if (null != list && !list.isEmpty()) {
        	resultList = list.subList(pageStartRow, pageEndRow);
        }
        return resultList;
    }
 
    /**
     * 判断要不要分页
     * @return
     */
    public boolean isNext() {
        return list.size() > pageRecorders;
    }

    /**
     * 获取第一页的内容
     * @return
     */
    public List<?> getFistPage() {
        if (this.isNext()) {
            return list.subList(0, pageRecorders);
        } else {
            return list;
        }
    }
    
    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public int getPage() {
        return pageNum;
    }

    public void setPage(int page) {
        this.pageNum = page;
    }

    public int getPageEndRow() {
        return pageEndRow;
    }

    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }

    public int getPageRecorders() {
        return pageRecorders;
    }

    public void setPageRecorders(int pageRecorders) {
        this.pageRecorders = pageRecorders;
    }

    public int getPageStartRow() {
        return pageStartRow;
    }

    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public boolean isHasPreviousPage() {
        return hasPreviousPage;
    }
    
    public void setHasPreviousPage(boolean hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }
}
